<?php declare(strict_types=1);


namespace PinWuYun\domain\service\messageServer\actions;

use Exception;
use GuzzleHttp\Exception\GuzzleException;
use PinWuYun\domain\dao\AreaDao;
use PinWuYun\domain\dao\OrderRelationDao;
use PinWuYun\domain\data\YouZanData\OrderData;
use PinWuYun\domain\service\config\config;
use PinWuYun\domain\service\messageServer\messageImpl;
use PinWuYun\domain\service\starLinkServer\Order;
use Youzan\Open\Helper\CryptoHelper;

/**
 * Created: by zhiJie
 * Date: 2021/7/2
 */
class TradePaid implements messageImpl
{

    /**
     * @throws GuzzleException
     * @throws Exception
     */
    public function doAction(array $requestParams)
    {
        /** 密文解码 */
        $this->decode($requestParams);
        /** 下单支付完成 */
        $full_order_info = $requestParams['msg']['full_order_info'];
        $delivery_city = $full_order_info['address_info']['delivery_city'];
        $delivery_province = $full_order_info['address_info']['delivery_province'];
        $delivery_district = $full_order_info['address_info']['delivery_district'];
        /** 获取区域id */
        $requestParams['regionId'] = (new AreaDao)->getAreaId($delivery_province, $delivery_city, $delivery_district);
        /** 订单数据填充处理 */
        $requestParams = (new OrderData)->buildOrderInfo($requestParams);

        /** 预占库存,并下单 */
        $orderResponse = (new Order)->preHoleAndAddOrder($requestParams);
        $data = $orderResponse['data'];
        $attr = [
            'request_data' => json_encode($requestParams['orderInfo']),
            'you_tid' => $full_order_info['order_info']['tid'],
            'star_order_sn' => $data['orderSn'],
            'status' => $data['status'],
            'status_name' => $data['statusName'],
        ];
        (new OrderRelationDao)->insert($attr);

    }


    /**
     * 把加密信息解码
     * @param $requestParams
     */
    public function decode(&$requestParams)
    {
        $secret = (new config)->get('client_secret');
        // 详细地址
        $delivery_address = &$requestParams['msg']['full_order_info']['address_info']['delivery_address'];
        $delivery_address = CryptoHelper::decrypt($delivery_address, $secret);
        //  收货人姓名
        $receiver_name = &$requestParams['msg']['full_order_info']['address_info']['receiver_name'];
        $receiver_name = CryptoHelper::decrypt($receiver_name, $secret);
        //收货人手机号
        $receiver_tel = &$requestParams['msg']['full_order_info']['address_info']['receiver_tel'];
        $receiver_tel = CryptoHelper::decrypt($receiver_tel, $secret);
        //  到店自提信息 json格式
        $self_fetch_info = &$requestParams['msg']['full_order_info']['address_info']['self_fetch_info'];
        $self_fetch_info = CryptoHelper::decrypt($self_fetch_info, $secret);
        // 买家手机号
        $buyer_phone = &$requestParams['msg']['full_order_info']['buyer_info']['buyer_phone'];
        $buyer_phone = CryptoHelper::decrypt($buyer_phone, $secret);
        //粉丝昵称
        $fans_nickname = &$requestParams['msg']['full_order_info']['buyer_info']['fans_nickname'];
        $fans_nickname = CryptoHelper::decrypt($fans_nickname, $secret);
    }
}